package com.example.concurrentprogramming8016.reetrantlock;

import lombok.extern.slf4j.Slf4j;

import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.ReentrantLock;

/**
 * @author liyanhao
 * @Date 2023-01-05 18:38
 * 可中断
 */
@Slf4j
public class ReentrantLockDemo3 {
    private static ReentrantLock reentrantLock = new ReentrantLock();

    public static void main(String[] args) throws InterruptedException {
        Thread thread = new Thread(() -> {
            log.debug("t1启动了....");
            try {
                //判断一下中断的状态 如果该线程被执行中断,则获取锁的时候就会直接报错
                reentrantLock.lockInterruptibly();
                log.debug("t1获得了锁");
            } catch (InterruptedException e) {
                log.debug("调用报错了");
                throw new RuntimeException(e);
            } finally {
                reentrantLock.unlock();
            }

        }, "t1");
            reentrantLock.lock();
        try {
            log.debug("main线程获得了锁");
            thread.start();
            //先让线程t1执行
            TimeUnit.SECONDS.sleep(5);
            thread.interrupt();
            log.debug("线程t1执行中断了");
        }finally {
            reentrantLock.unlock();
        }




    }
}
